#! /bin/gawk -f
# qsplitr: Script to extract split restraint file in various sub-files
# according to type of content.
# START_OF_..._DATA or START_OF_MODEL indicates content switch.
# Data between END_OF_DATA and START_... are written to stdout.
# Sub-files are named 'ident.raw_...'.
# Input variables: 
#   ident:   file name (directory path and prefix)
# =============================================================
# Ton Rullmann, April 12, 1996
# =============================================================

BEGIN {
    if ( ARGC <= 1 ) {
	print "usage: qsplitr ident='XX' file"
	exit ( 1 )
    }
    do_upper_dis = 0
    do_upper_noe = 0
    do_upper_hb  = 0
    do_upper_ss  = 0
    do_lower_dis = 0
    do_lower_noe = 0
    do_lower_hb  = 0
    do_lower_ss  = 0
    do_upplow_dis = 0
    do_upplow_noe = 0
    do_upplow_hb  = 0
    do_upplow_ss  = 0
    copy_raw = "false"
    file_raw     = ident ".raw"
    file_raw_dis = ident ".raw_dis"
    file_raw_noe = ident ".raw_noe"
    file_raw_tor = ident ".raw_tor"
    file_raw_ss  = ident ".raw_ss"
    file_raw_hb  = ident ".raw_hb"
    file_raw_mdl = ident ".raw_mdl"
    file_raw_text = ident ".raw_text"
    raw_dis_count = 0
    raw_noe_count = 0
    raw_hb_count  = 0
    raw_ss_count  = 0
    raw_tor_count = 0
    raw_mdl_count = 0
    raw_text_count = 0
    system( "/bin/rm -f "file_raw" "file_raw_dis" "file_raw_noe" "file_raw_tor" "file_raw_ss" "file_raw_hb" "file_raw_mdl " "file_raw_text )
}

function do_raw() {
    print >> file_raw
    raw_count++
}

function do_raw_dis() {
    if ( do_upper_dis ) {
	print "# UPPER BOUND DATA" >> file_raw_dis
	do_upper_dis = 0
    }
    else if ( do_lower_dis ) {
	print "# LOWER BOUND DATA" >> file_raw_dis
	do_lower_dis = 0
    }
    else if ( do_upplow_dis ) {
	print "# UPPLOW BOUND DATA" >> file_raw_dis
	do_upplow_dis = 0
    }
    print >> file_raw_dis
    raw_dis_count++
}

function do_raw_noe() {
    if ( do_upper_noe ) {
	print "# UPPER BOUND DATA" >> file_raw_noe
	do_upper_noe = 0
    }
    else if ( do_lower_noe ) {
	print "# LOWER BOUND DATA" >> file_raw_noe
	do_lower_noe = 0
    }
    else if ( do_upplow_noe ) {
	print "# UPPLOW BOUND DATA" >> file_raw_noe
	do_upplow_noe = 0
    }
    print >> file_raw_noe
    raw_noe_count++
}

function do_raw_hb() {
    if ( do_upper_hb ) {
	print "# UPPER BOUND DATA" >> file_raw_hb
	do_upper_hb = 0
    }
    else if ( do_lower_hb ) {
	print "# LOWER BOUND DATA" >> file_raw_hb
	do_lower_hb = 0
    }
    else if ( do_upplow_hb ) {
	print "# UPPLOW BOUND DATA" >> file_raw_hb
	do_upplow_hb = 0
    }
    print >> file_raw_hb
    raw_hb_count++
}

function do_raw_ss() {
    if ( do_upper_ss ) {
	print "# UPPER BOUND DATA" >> file_raw_ss
	do_upper_ss = 0
    }
    else if ( do_lower_ss ) {
	print "# LOWER BOUND DATA" >> file_raw_ss
	do_lower_ss = 0
    }
    else if ( do_upplow_ss ) {
	print "# UPPLOW BOUND DATA" >> file_raw_ss
	do_upplow_ss = 0
    }
    print >> file_raw_ss
    raw_ss_count++
}

function do_raw_tor() {
    print >> file_raw_tor
    raw_tor_count++
}

function do_raw_mdl() {
    print >> file_raw_mdl
    raw_mdl_count++
}

$1 ~ /START_OF_DATA/ || $1 ~ /START_OF_OTH_DATA/ {
    copy_raw = "true"
    next
}

$1 ~ /START_OF_DIS_DATA/ {
    copy_raw = "dis"
    next
}

$1 ~ /START_OF_NOE_DATA/ {
    copy_raw = "noe"
    next
}

$1 ~ /START_OF_HB_DATA/ {
    copy_raw = "hb"
    next
}

$1 ~ /START_OF_SS_DATA/ {
    copy_raw = "ss"
    next
}

$1 ~ /START_OF_TOR_DATA/ || $1 ~ /START_OF_DIH_DATA/ {
    copy_raw = "tor"
    next
}

$1 ~ /END_OF_DATA/ {
    copy_raw = "false"
    next
}

$1 ~ /START_OF_MODEL/ {
    copy_raw = "mdl"
# write this line too! (useful in convMRTABLE)
#    next
}

$1 ~ /UPPER_BOUND_DATA/ {
    do_upper_dis = 1
    do_upper_noe = 1
    do_upper_hb  = 1
    do_upper_ss  = 1
    do_lower_dis = 0
    do_lower_noe = 0
    do_lower_hb  = 0
    do_lower_ss  = 0
    do_upplow_dis = 0
    do_upplow_noe = 0
    do_upplow_hb  = 0
    do_upplow_ss  = 0
    next
}

$1 ~ /LOWER_BOUND_DATA/ {
    do_upper_dis = 0
    do_upper_noe = 0
    do_upper_hb  = 0
    do_upper_ss  = 0
    do_lower_dis = 1
    do_lower_noe = 1
    do_lower_hb  = 1
    do_lower_ss  = 1
    do_upplow_dis = 0
    do_upplow_noe = 0
    do_upplow_hb  = 0
    do_upplow_ss  = 0
    next
}

$1 ~ /UPPLOW_BOUND_DATA/ {
    do_upper_dis = 0
    do_upper_noe = 0
    do_upper_hb  = 0
    do_upper_ss  = 0
    do_lower_dis = 0
    do_lower_noe = 0
    do_lower_hb  = 0
    do_lower_ss  = 0
    do_upplow_dis = 1
    do_upplow_noe = 1
    do_upplow_hb  = 1
    do_upplow_ss  = 1
    next
}

# Begin-of-general actions
    {

    if ( copy_raw == "true" ) {
	do_raw()
        next
    }
    if ( copy_raw == "dis" ) {
	do_raw_dis()
        next
    }
    if ( copy_raw == "noe" ) {
	do_raw_noe()
        next
    }
    if ( copy_raw == "hb" ) {
	do_raw_hb()
        next
    }
    if ( copy_raw == "ss" ) {
	do_raw_ss()
        next
    }
    if ( copy_raw == "tor" ) {
	do_raw_tor()
        next
    }
    if ( copy_raw == "mdl" ) {
	do_raw_mdl()
        next
    }
}
# End-of-general actions

# Write the experimental data to the various .raw files (DISGEO keywords)

$1 ~ /NOEUPP/ {
    do_raw_noe()
    next
}

$1 ~ /NOELOW/ {
    do_raw_noe()
    next
}

$1 ~ /HBUPP/ {
    do_raw_hb()
    next
}

$1 ~ /HBLOW/ {
    do_raw_hb()
    next
}

$1 ~ /SSUPP/ {
    do_raw_ss()
    next
}

$1 ~ /SSLOW/ {
    do_raw_ss()
    next
}

$1 ~ /ANGLE/ {
    do_raw_tor()
    next
}

$1 ~ /JCOUPL/ {
    do_raw()
    next
}

$1 ~ /SHIFTS/ {
    do_raw()
    next
}

# Catch comment lines DISGEO (idents never used) and comment lines
# appearing before the first START_OF... or between END_OF_DATA and START_OF...

$1 ~ /^[#!*]/ && copy_raw == "false" {
    print >> file_raw_text
    raw_text_count++
    next
}    

# Print all remaining lines

NF == 0 {
    next
}

    {
    print "* Skipped line: " $0
    unk++
}

END {
    printf( "* %6i lines copied to %s\n", raw_dis_count, file_raw_dis )
    printf( "* %6i lines copied to %s\n", raw_noe_count, file_raw_noe )
    printf( "* %6i lines copied to %s\n", raw_hb_count, file_raw_hb )
    printf( "* %6i lines copied to %s\n", raw_ss_count, file_raw_ss )
    printf( "* %6i lines copied to %s\n", raw_tor_count, file_raw_tor )
    printf( "* %6i lines copied to %s\n", raw_count, file_raw )
    printf( "* %6i lines copied to %s\n", raw_mdl_count, file_raw_mdl )
    if ( raw_text_count )
	printf( "* %6i lines copied to %s\n", raw_text_count, file_raw_text )
    printf( "* %6i remaining lines\n", unk )
}
